package com.fingard.dsp.bank.directbank.bfb01;

import java.util.HashMap;
import java.util.Map;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;

public class BFB01RealTime extends BFB01Base implements IRealTime {

	// 单笔，不需采用流处理方式
	public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
		// 返回ATS的对象
		RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
		// 取第三方商户
		ActSetItem actItem = getBankConfig().getActSetItem(p_req9108.singleDetail.recAct, "");
		if (actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath)) {
			RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
			tmpRetDetail.respCode = "0";
			if (actItem == null) {
				tmpRetDetail.respInfo = "未找到账号" + p_req9108.singleDetail.recAct + "的批量商户配置节点";
			} else {
				tmpRetDetail.respInfo = "账号" + p_req9108.singleDetail.recAct + "的证书文件不存在";
			}
			tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
			tmpRetDetail.transState = PaymentState.FAIL_Batch;
			tmpRetDetail.payInfoCode = FailCode.E9981;
			retAts.writeRetDetail(tmpRetDetail);
			return retAts;
		}
		HashMap<String, String> sendMap=null;
		String[] tmpStrRet ={"",""};
		if(p_req9108.reqHeader.commandCode.equals("910801")){
			sendMap=d_910801(p_req9108, actItem);
			tmpStrRet = sendToBank(sendMap, actItem);
		}
		else if(p_req9108.reqHeader.commandCode.equals("910802")){
			sendMap=d_910802(p_req9108, actItem);
			tmpStrRet = sendToBank(sendMap, actItem);
		}
		if(sendMap==null){
			throw new Exception("commandCode标签内容没有找到相应接口");
		}
		
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		// 处理银行返回的数据
		if (tmpStrRet[0].length() == 0) {
			Map<String, String> map = getRet(tmpStrRet[1]);
			if (map != null && map.size() != 0) {
				tmpRetDetail.respCode = "0";
				tmpRetDetail.respInfo = map.get("respMsg");
				tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
				tmpRetDetail.transState = map.get("respCode");
				tmpRetDetail.payInfoCode = tmpRetDetail.transState;
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "返回数据格式错误";
			}
		} else if (tmpStrRet[0].equals(ConnState.FAIL)) {
			tmpRetDetail.respCode = "0";
			tmpRetDetail.respInfo = tmpStrRet[1];
			tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
			tmpRetDetail.transState = PaymentState.FAIL_Payment;
			tmpRetDetail.payInfoCode = tmpStrRet[0];
		} else {
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = tmpStrRet[1];
		}
		if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
			tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
		}
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	// 9144 状态查询
	public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
		// 返回ATS的对象
		RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
		RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
		try {
			HashMap<String, String> sendMap=new HashMap<String, String>();
			sendMap.put("version", "1.0");
			sendMap.put("transCode", "orderQuery");
			sendMap.put("signType", "01");
			// 取第三方商户
			ActSetItem actItem = getBankConfig().getActSetItem(p_req9144.recAct, "");
			if (actItem == null || !FileHelper.existFile(actItem.ownKeyStorePath)) {
				RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
				tmpRetDetail.respCode = "0";
				if (actItem == null) {
					tmpRetDetail.respInfo = "未找到账号" + p_req9144.recAct + "的批量商户配置节点";
				} else {
					tmpRetDetail.respInfo = "账号" + p_req9144.payAct + "的证书文件不存在";
				}
				tmpRetDetail.reqSeqID = p_req9144.askNumber;
				tmpRetDetail.transState = PaymentState.FAIL_Batch;
				tmpRetDetail.payInfoCode = FailCode.E9981;
				retAts.writeRetDetail(tmpRetDetail);
				return retAts;
			}
			sendMap.put("merchantId", actItem.merchantId);
			sendMap.put("mcTransDateTime", Format.DateTime14Format.format(p_req9144.getBgnDateTime()));
			sendMap.put("orderNo",  p_req9144.askNumber);
			if(p_req9144.payAmount!=null){
				sendMap.put("amount", StringHelper.amountMultiply100(p_req9144.payAmount));
			}else{
				RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "金额不能为空";
				tmpRetDetail.reqSeqID = p_req9144.askNumber;
				retAts.writeRetDetail(tmpRetDetail);
				return retAts;
			}
			
			String[] tmpStrRet = sendToBank(sendMap, actItem);
			if (tmpStrRet[0].length() == 0) {
				Map<String, String> map = getRet(tmpStrRet[1]);
				if (map != null && map.size() != 0) {
					// 受理成功
					if (map.get("respCode").equals("B0")) {
						if (map.get("orderNo").equals(p_req9144.askNumber)) {
							tmpRetAtsDetail.respCode = "0";
							tmpRetAtsDetail.respInfo = map.get("ordMsg");
							tmpRetAtsDetail.transState = map.get("respCode") + "+" + map.get("ordSts");
							tmpRetAtsDetail.reqSeqID = p_req9144.askNumber;
							tmpRetAtsDetail.payInfoCode = map.get("ordSts");
						} else {
							tmpRetAtsDetail.respCode = "-1";
							tmpRetAtsDetail.respInfo = "返回的流水号" + map.get("orderNo") + "与请求" + p_req9144.askNumber
									+ "不符";
						}
					} else {
						tmpRetAtsDetail.respCode = "-1";
						tmpRetAtsDetail.respInfo = map.get("respMsg");
					}
				} else {
					tmpRetAtsDetail.respCode = "-1";
					tmpRetAtsDetail.respInfo = "返回数据格式错误";
				}
			} else {
				tmpRetAtsDetail.respCode = "-1";
				tmpRetAtsDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception e) {
			tmpRetAtsDetail.respCode = "-1";
			tmpRetAtsDetail.respInfo = e.getMessage();
			e.printStackTrace();
		}
		retAts.writeRetDetail(tmpRetAtsDetail);
		return retAts;
	}

	//实时收款
	private HashMap<String, String> d_910801(ReqSingle p_req9108,ActSetItem actItem) throws Exception{
		HashMap<String,String> map=new HashMap<String, String>();
		map.put("version", "1.0");
		map.put("transCode", "singleCollection");
		map.put("signType", "01");
		map.put("merchantId",actItem.merchantId);
		map.put("mcSequenceNo", p_req9108.singleDetail.askNumber);
		map.put("mcTransDateTime", Format.DateTime14Format.format(p_req9108.singleDetail.getPayDateTime()));
		map.put("orderNo", p_req9108.singleDetail.askNumber);
		map.put("amount", StringHelper.amountMultiply100(p_req9108.singleDetail.payAmount));
		map.put("cardNo", p_req9108.singleDetail.payAct);
		map.put("accName", p_req9108.singleDetail.payName);
		if (p_req9108.singleDetail.isPrivate.equals("1")) {
			if (p_req9108.singleDetail.cardType.equals("1")) {
				map.put("accType","1");
			} else {
				map.put("accType","0");
			}
		} else if (p_req9108.singleDetail.isPrivate.equals("2")) {
			map.put("accType","3");
		}
		map.put("lBnkNo","0" + p_req9108.singleDetail.payBankCode);
		map.put("lBnkNam",p_req9108.singleDetail.payBankName);
		map.put("idType",convertCertTypeToBank(p_req9108.singleDetail.certType));
		map.put("idInfo",p_req9108.singleDetail.certNum);
		map.put("validPeriod",p_req9108.singleDetail.expiredDate);
		map.put("cvv2",p_req9108.singleDetail.cvv2);
		map.put("remark",p_req9108.singleDetail.memo);
		map.put("capUse","00");
		return map;
	}
	
	//快捷支付
	private HashMap<String, String> d_910802(ReqSingle p_req9108,ActSetItem actItem) throws Exception{
		HashMap<String, String> map=new HashMap<String, String>();
		map.put("charset", getCharset());
		map.put("version", "1.0");
		map.put("merchantCert", "");
		map.put("signType", "RSA");
		map.put("service", "FirstQPayment");
		map.put("offlineNotifyUrl", "");
		map.put("requestId",p_req9108.singleDetail.askNumber);
		map.put("merchantId", actItem.merchantId);
		map.put("orderId", "");
		map.put("orderTime",Format.DateTime14Format.format(p_req9108.singleDetail.getPayDateTime()));
		map.put("cardNo", p_req9108.singleDetail.recAct);//收方？
		map.put("idInfo",p_req9108.singleDetail.certNum);
		map.put("idType",getBankConfig().getMapValueByKey("RealTimecertType", p_req9108.singleDetail.certType));
		map.put("cardName",p_req9108.singleDetail.recName);
		map.put("expiredDate",p_req9108.singleDetail.expiredDate);
		map.put("cvv2",p_req9108.singleDetail.cvv2);
		map.put("cardPhone","");
		map.put("totalAmount",StringHelper.amountMultiply100(p_req9108.singleDetail.payAmount));
		map.put("currency",convertCurToBank(p_req9108.singleDetail.getOppCur()));
		map.put("productName","");
		return map;
	}
}
